#coding:utf-8
import os
from common.koufuApi import KoufuApi
from common.koufuShopApi import KoufuShopApi
from common.tool import Tools

__author__ = 'yangxianming'

from common.KoufuConf import KoufuConf
import json
import requests


def case():
    #初始化必要的参数
    kou_fu_conf = KoufuConf()
    # test_url = kou_fu_conf.koufu_test_domain + "/api/koufu"
    test_mng_url = kou_fu_conf.koufu_back_test + "/web/mng"
    test_url = kou_fu_conf.shop_domain
    kou_fu_shop_api = KoufuShopApi()
    kou_fu_api = KoufuApi()
    script_name = os.path.basename(__file__)
    tool = Tools
    tf = True
    test_result = ""

    #商城脚本
    #授权获得token
    # p = kou_fu_shop_api.get_api_auth_koufu_authorize_data(update_body={"thirdUserId":"25244122401072542"})
    # r = requests.post(test_url+kou_fu_shop_api.get_api_auth_koufu_authorize_data(get_path=1), data=json.dumps(p), headers=kou_fu_conf.shop_headers)
    # print("这个是接口api_auth_koufu_authorize返回的全部参数\n", r.content)
    # token = r.json()["return_data"]["token"]
    # kou_fu_conf.shop_headers.update({"X-Efulishop-Token":token})
    #拿到付款前福豆余额
    # p = kou_fu_shop_api.get_api_pay_payment_data()
    # r = requests.post(test_url+kou_fu_shop_api.get_api_pay_payment_data(get_path=1), data=json.dumps(p), headers=kou_fu_conf.shop_headers)
    # print("这个是接口api_pay_payment返回的全部参数\n", r.content)
    # before_balance = r.json()["return_data"]["balance"]

    #首页分类
    # p = kou_fu_shop_api.get_catagory_all_data()
    # print(p)
    # r = requests.post(test_url+kou_fu_shop_api.get_catagory_all_data(get_path=1), data=json.dumps(p), headers=kou_fu_conf.shop_headers)
    # print("[catagory_all]:\n", r.content.decode("utf-8"))

    #商城后管脚本
    #循环添加分类
    picture_uri = {}
    for line in open("text1.txt"):
        line = str(line).strip()
        a = str(line).split(".")
        b = str(line).split("/")
        picture_uri[a[0]] = b[5]
        # print(a[0] + b[5])

    print(picture_uri)

    tpye = {"家用电器-1036047":["电饭煲-dfbb0530290d808a256220f120ff1763","电火锅-dfbb0530290d808a256220f120ff1763","电水壶-dfbb0530290d808a256220f120ff1763","煎烤电器-dfbb0530290d808a256220f120ff1763","咖啡机-dfbb0530290d808a256220f120ff1763","早餐机-dfbb0530290d808a256220f120ff1763","保健按摩-dfbb0530290d808a256220f120ff1763","电吹风-dfbb0530290d808a256220f120ff1763","电子秤-dfbb0530290d808a256220f120ff1763","口腔清洁-dfbb0530290d808a256220f120ff1763","美发器-dfbb0530290d808a256220f120ff1763","电风扇-dfbb0530290d808a256220f120ff1763","挂烫机熨斗-dfbb0530290d808a256220f120ff1763","加湿器-dfbb0530290d808a256220f120ff1763","空气净化器-dfbb0530290d808a256220f120ff1763","取暖器-dfbb0530290d808a256220f120ff1763","吸尘器-dfbb0530290d808a256220f120ff1763"],
            "大牌卡券-1036048":["代金券-dfbb0530290d808a256220f120ff1763","会员卡-dfbb0530290d808a256220f120ff1763","影音会员-dfbb0530290d808a256220f120ff1763"],
            "居家餐厨-1036049":["保温杯-dfbb0530290d808a256220f120ff1763","茶具杯具-f10da86e7b4766fc1b0dd272c4920f90","刀具砧板-dfbb0530290d808a256220f120ff1763","饭盒-dfbb0530290d808a256220f120ff1763","锅具汤煲-dfbb0530290d808a256220f120ff1763","果汁杯-dfbb0530290d808a256220f120ff1763","咖啡杯-dfbb0530290d808a256220f120ff1763","马克杯-dfbb0530290d808a256220f120ff1763","焖烧杯-dfbb0530290d808a256220f120ff1763","碗筷厨具-dfbb0530290d808a256220f120ff1763","吸管杯-dfbb0530290d808a256220f120ff1763"],
            "日化洗护-1036050":["口腔清洁-dfbb0530290d808a256220f120ff1763","旅行套装-f10da86e7b4766fc1b0dd272c4920f90","沐浴露-dfbb0530290d808a256220f120ff1763","清洁养护-dfbb0530290d808a256220f120ff1763","驱蚊用品-dfbb0530290d808a256220f120ff1763","湿巾-dfbb0530290d808a256220f120ff1763","梳子-dfbb0530290d808a256220f120ff1763","洗发水-dfbb0530290d808a256220f120ff1763","洗护套装-dfbb0530290d808a256220f120ff1763","洗脸巾-dfbb0530290d808a256220f120ff1763"],
            "箱包-1036051":["旅行箱-f10da86e7b4766fc1b0dd272c4920f90","双肩包-f10da86e7b4766fc1b0dd272c4920f90"],
            "3C数码-1036052":["充电宝-f10da86e7b4766fc1b0dd272c4920f90","耳机-f10da86e7b4766fc1b0dd272c4920f90","配件套装-dfbb0530290d808a256220f120ff1763","数据线-dfbb0530290d808a256220f120ff1763","音箱-dfbb0530290d808a256220f120ff1763"],
            "美妆-1036053":["护肤品-f10da86e7b4766fc1b0dd272c4920f90","洁面-f10da86e7b4766fc1b0dd272c4920f90","精华-dfbb0530290d808a256220f120ff1763","面膜-dfbb0530290d808a256220f120ff1763","乳液面霜-dfbb0530290d808a256220f120ff1763","眼霜-dfbb0530290d808a256220f120ff1763"],
            "酒水饮品-1036054":["茶-f10da86e7b4766fc1b0dd272c4920f90","咖啡-f10da86e7b4766fc1b0dd272c4920f90","矿泉水-dfbb0530290d808a256220f120ff1763","奶茶-dfbb0530290d808a256220f120ff1763","乳品-dfbb0530290d808a256220f120ff1763","其他饮品-dfbb0530290d808a256220f120ff1763"],
            "居家配饰-1036055":["抱枕坐垫-f10da86e7b4766fc1b0dd272c4920f90","床品套件-f10da86e7b4766fc1b0dd272c4920f90","创意摆件-dfbb0530290d808a256220f120ff1763","节日礼品-dfbb0530290d808a256220f120ff1763","毛巾浴巾-dfbb0530290d808a256220f120ff1763","雨具-dfbb0530290d808a256220f120ff1763"],
            "休闲食品-1036056":["冰淇淋-f10da86e7b4766fc1b0dd272c4920f90","糕点饼干-f10da86e7b4766fc1b0dd272c4920f90","果脯坚果-dfbb0530290d808a256220f120ff1763","小食甜巧-dfbb0530290d808a256220f120ff1763","肉干熟食-dfbb0530290d808a256220f120ff1763","方便速食-dfbb0530290d808a256220f120ff1763","火锅-dfbb0530290d808a256220f120ff1763","面条-dfbb0530290d808a256220f120ff1763"],
            "饮料冲饮-1036057":["谷物麦片-f10da86e7b4766fc1b0dd272c4920f90","果酱-f10da86e7b4766fc1b0dd272c4920f90"],
            "礼盒-1036058":["干货礼盒-f10da86e7b4766fc1b0dd272c4920f90","节日礼品-f10da86e7b4766fc1b0dd272c4920f90"]}

    picture_domain = "https://efuli.oss-cn-hangzhou.aliyuncs.com/pic/"
    for k,v in tpye.items():
        name,name_id = str(k).split("-")
        print(name+name_id)
        for one in v:
            print(one)
            name,uri = str(one).split("-")
            if name in picture_uri:
               uri = picture_uri[name]
            p = kou_fu_shop_api.get_category_save_data(update_body={"name":name,"frontDesc":name,"parentId":name_id,"imgUrl":picture_domain+uri})
            print(p)
            r = requests.post(test_url+kou_fu_shop_api.get_category_save_data(get_path=1), data=json.dumps(p), headers=kou_fu_conf.koufu_shop_headers)
            print("[category_save]:\n", r.content.decode("utf-8"))



    # p = kou_fu_shop_api.get_category_save_data(update_body={"name":"电水壶","frontDesc":"电水壶","parentId":1036047})
    # print(p)
    # r = requests.post(test_url+kou_fu_shop_api.get_category_save_data(get_path=1), data=json.dumps(p), headers=kou_fu_conf.koufu_shop_headers)
    # print("[category_save]:\n", r.content.decode("utf-8"))

    ##################################################

    #首页口福场景查询
    # p = kou_fu_api.get_efuli_koufu_corp_scene_list_query_corpId()
    # print(p)
    # r = requests.post(test_url, data=json.dumps(p), headers=kou_fu_conf.headers)
    # print("[efuli_koufu_corp_scene_list_query_corpId]:\n", r.content.decode("utf-8"))


    #口福后管-分页查询口福企业场景 action = koufu.corp.scene.query.page
    # p = kou_fu_api.get_koufu_corp_scene_query_page()
    # r = requests.post(test_mng_url, data=json.dumps(p), headers=kou_fu_conf.koufu_back_head_test)
    # print("[koufu.corp.scene.query.page]\n", r.content.decode("utf-8"))

    #场景绑定企业列表分页查询 action=koufu.corp.scene.corp.list.query.page
    # p = kou_fu_api.get_koufu_corp_scene_corp_list_query_page(update_body={"sceneId":"48fde48d060d4695846b4f058cd9628f"})
    # r = requests.post(test_mng_url, data=json.dumps(p), headers=kou_fu_conf.koufu_back_head_test)
    # print("[koufu.corp.scene.corp.list.query.page]\n", r.content.decode("utf-8"))

    #场景绑定企业 action = koufu.corp.scene.corp.bind
    # p = kou_fu_api.get_koufu_corp_scene_corp_bind(update_body={"corpId":"dingf686af244fa39dcf35c2f4657eb6378f","sceneId":"48fde48d060d4695846b4f058cd9628f"})
    # r = requests.post(test_mng_url, data=json.dumps(p), headers=kou_fu_conf.koufu_back_head_test)
    # print("[koufu.corp.scene.corp.bind]\n", r.content.decode("utf-8"))



    #定义预期描述
    print(str(tf))
    # print(test_result)
    expect_result = "返回参数ebuyCode是否是空"

    #返回测试结果 第一个参数:True or False    第二个参数测试结果描述
    return tf, [test_result, expect_result]

case()